<?php
/**
 * 真题管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\exercise\controllers;


use yii;
use app\libs\ApiControl;
use app\modules\exercise\models\ExerciseBanner;

class BannerController extends ApiControl {
    public $layout = 'exercise';
    public $enableCsrfValidation = false;
    /**
     * [actionIndex description]
     * @Title  [title]
     * @Author Don
     * @Date   2021-05-12
     * @return [type]     [description]
     */
    public function actionIndex()
    {
        $data = ExerciseBanner::find()->asArray()->all();
        return $this->render('index',[
            'data' => $data,
            // 'block' => $this->block
        ]);
    }

    /**
     * [actionUpdate description]
     * @Title  [title]
     * @Author Don
     * @Date   2021-05-12
     * @return [type]     [description]
     */
    public function actionUpdate()
    {
        if ($_POST) {
            // f_d($_POST);
            $id = Yii::$app->request->post('id');
            if ($id) {
                $model = ExerciseBanner::findOne($id);
                $model->name = Yii::$app->request->post('name');
                $model->typeid = Yii::$app->request->post('typeid');
                $model->image1 = Yii::$app->request->post('image1');
                $model->image_url1 = Yii::$app->request->post('image_url1');
                $model->image2 = Yii::$app->request->post('image2');
                $model->image_url2 = Yii::$app->request->post('image_url2');
                if ($model->save()) {
                    $this->redirect('/exercise/banner/index');
                } else {
                    die('<script>alert("保存失败，请重试");history.go(-1);</script>');
                }
            } else {
                $model = new ExerciseBanner();
                $model->name = Yii::$app->request->post('name');
                $model->typeid = Yii::$app->request->post('typeid');
                $model->image1 = Yii::$app->request->post('image1');
                $model->image_url1 = Yii::$app->request->post('image_url1');
                $model->image2 = Yii::$app->request->post('image2');
                $model->image_url2 = Yii::$app->request->post('image_url2');
                if ($model->save()) {
                    $this->redirect('/exercise/banner/index');
                } else {
                    die('<script>alert("保存失败，请重试");history.go(-1);</script>');
                }
            }
            
        } else {
            $id = Yii::$app->request->get('id');
            $data = ExerciseBanner::findOne($id);
            // f_d($data);
            return $this->render('update',[
                'data' => $data
            ]);

        }


    }
}